import { getRulesList, fetchRulesDetail, delRules, addRules, companyTree } from '@/services/notice/rules';
import { getDict } from '@/services/myRecruit/talentPool';
import { getList } from '@/services/sysRules/rules';

export default {
    namespace: 'rules',
    state: {
        data: [],
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        changeData: {
            id: null,
            content: null,
            createUser: '',
            recvCompany: '',
            startTime: null,
            detail: null,
            title: ''
        },
        delateResult: false,
        companyTree: [],
        rulesTableList: {}
    },
    effects: {
        *fetchList({ payload, callback }, { put, call }) {
            const result = yield call(getRulesList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        data: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchDetail({ payload, callback }, { put, call }) {
            const result = yield call(fetchRulesDetail, payload);
            yield put({
                type: 'save',
                payload: {
                    changeData: result.data
                }
            });
            if (callback) callback(result);
        },
        *fetchAdd({ payload, callback }, { put, call }) {
            const result = yield call(addRules, payload);
            yield put({
                type: 'save',
                payload: {
                    changeData: result.data
                }
            });
            if (callback) callback(result);
        },
        *fetchDelate({ payload, callback }, { call }) {
            const result = yield call(delRules, payload);
            if (callback) callback(result);
        },
        *fetchCompanyTree({ payload, callback }, { put, call }) {
            const result = yield call(companyTree, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        companyTree: result.data
                    }
                });
            }
            if (callback) callback(result);
        },
        // 以下为修改后的规章制度
        *featchRuleType({ payload, callback }, { call, put }) {
            const result = yield call(getDict, payload);
            if (result && result.code === 10000) {
                // 因为字典里没有全部制度，所以插入了一个
                yield put({
                    type: 'save',
                    payload: {
                        ruleType: result.data
                    }
                });
                if (callback) callback(result.data);
            }
        },
        // 规章制度列表
        *featchList({ payload, callback }, { call }) {
            const result = yield call(getList, payload);
            if (result && result.code === 10000) {
                if (callback) callback(result.data);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        }
    }
};
